﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using Pong_Project.Game_objects;

namespace Pong_Project
{
    class FileReader
    {
        public static List<Level> loadLevelSet()
        {
            List<Level> levelSet = new List<Level>();
            // Create an instance of StreamReader to read from a file.
            // The using statement also closes the StreamReader.
            using (StreamReader sr = new StreamReader("levels.txt"))
            {
                bool readLevelLine = false;
                int row = 0;
                char[,] board = new char[14, 7];
                // Read lines from the file until the end of
                // the file is reached.
                while (!sr.EndOfStream)
                {
                    var line = sr.ReadLine();
                    if (line.Length > 0)
                    {
                        if (line.StartsWith("-"))
                        {
                            if (readLevelLine)
                            {
                                levelSet.Add(decodeLevel(board));
                            }
                            else
                            {
                                board = new char[14, 7];
                                row = 0;
                            }
                            readLevelLine = !readLevelLine;
                        }
                        else if (line.StartsWith("|") && readLevelLine)
                        {
                            for (int i = 1; i < line.Length - 1; i++)
                            {
                                board[row, i - 1] = line[i];
                            }
                            row++;
                        }
                    }
                }
            }
            Pong_Project.Views.Settings.SetLevelEntries(levelSet.Count);
            return levelSet;
        }

        private static Level decodeLevel(char[,] board)
        {
            int maxRows = board.GetUpperBound(0);
            int maxCols = board.GetUpperBound(1);
            Level level = new Level();
            for (int i = 0; i <= maxRows; i++)
            {
                for (int j = 0; j <= maxCols; j++)
                {
                    if (board[i, j] == '#' && (i > 0 ? board[i - 1, j] != '#' : true) && (j > 0 ? board[i, j - 1] != '#' : true))
                    {
                        int obstaclesToMergeX = 1;
                        int obstaclesToMergeY = 1;

                        int i2 = i + 1;
                        int j2 = j + 1;

                        if (i2 <= maxRows && board[i2, j] == '#')
                        {
                            while (i2 <= maxRows && board[i2, j] == '#')
                            {
                                obstaclesToMergeY++;
                                i2++;
                            }
                        }
                        else if (j2 <= maxCols && board[i, j2] == '#')
                        {
                            while (j2 <= maxCols && board[i, j2] == '#')
                            {
                                obstaclesToMergeX++;
                                j2++;
                            }
                        }
                        float positionX = CollisionDetector.ObstacleAreaLeftBoundary + j * 35;
                        float positionY = i * 35;
                        float scaleX = 1.0f * obstaclesToMergeX;
                        float scaleY = 1.0f * obstaclesToMergeY;
                        level.AddObstacle(AbstractFactory.NewObject(positionX, positionY, scaleX, scaleY));
                    }
                }
            }
            return level;
        }
    }
}
